<?php

$key=key($_POST);
switch ($key) {
    case 'a':
        checkUsername();
        break;
    case 'b':
        checkEmail();
        break;
    case 'c':
        checkPassword();
        break;
    default:
        checkOther();
        break;
}

function checkUsername()
{
    $username = $_POST['username'];

    $preg = "/^[a-z][0-9a-z]{5,7}$/";

    $c = preg_match($preg, $username);

    if(!$c){
        $arr = array(
            'error'=>1,
            'info'=>'长度是以字母开头6-8位的字母或数字'
        );
        echo json_encode($arr);
        exit;
    }

    $users = include_once './user.php';

    foreach ($users as  $user) {
        if($user['username']==$username){
            $arr = array(
                'error'=>1,
                'info'=>'用户名已存在'
            );
            echo json_encode($arr);
            exit;
        }
    }

    $arr = array(
        'error'=>0,
        'info'=>'用户可用'
    );
    echo json_encode($arr);
    exit;
}

function checkEmail()
{
    $username = $_POST['email'];

    $preg = "/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/";

    $c = preg_match($preg, $username);

    if(!$c){
        $arr = array(
            'error'=>1,
            'info'=>'邮箱格式错误'
        );
        echo json_encode($arr);
        exit;
    }

    // $users = include_once './user.php';

    // foreach ($users as  $user) {
    // 	if($user['username']==$username){
    // 		$arr = array(
    // 		'error'=>1,
    // 		'info'=>'用户名已存在'
    // 		);
    // 		echo json_encode($arr);
    // 		exit;
    // 	}
    // }

    $arr = array(
        'error'=>0,
        'info'=>'邮箱可用'
    );
    echo json_encode($arr);
    exit;
}

function checkPassword()
{
    $username = $_POST['password'];

    $preg = "/^\w{6,}$/";

    $c = preg_match($preg, $username);

    if(!$c){
        $arr = array(
            'error'=>1,
            'info'=>'密码不合法'
        );
        echo json_encode($arr);
        exit;
    }
    $arr = array(
        'error'=>0,
        'info'=>'密码可用'
    );
    echo json_encode($arr);
    exit;
}

function checkOther()
{
    $arr = array(
        'error'=>3,
        'info'=>'传的参数不对'
    );
    echo json_encode($arr);
    exit;
}



?>